﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Collections.ObjectModel;

namespace BasharLulu.QuranBookmark.WP7
{
    public partial class Bookmark : PhoneApplicationPage
    {
        private int BookmarkSuraKey = 0;
        private int BookmarkAyaNumber = 0;

        private IList<GroupedOC<SuraViewModel>> AyaBySuraList;

        public Bookmark()
        {
            InitializeComponent();
        }

        private void ApplicationBarIconButton_Click_Accept(object sender, EventArgs e)
        {
            AppSettings.AddNewBookmark(BookmarkSuraKey, BookmarkAyaNumber);
            
            NavigationService.GoBack();
        }

        private void ApplicationBarIconButton_Click_Deny(object sender, EventArgs e)
        {
            NavigationService.GoBack();
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (!App.SuraViewModel.IsDataLoadedOC)
            {
                App.SuraViewModel.LoadDataOC();
            }

            var AyatBySura = from sura in App.SuraViewModel.ItemsOC
                             group sura by sura.SuraNumber into c
                             select new GroupedOC<SuraViewModel>(c.Key, c);

            this.AyaBySuraList = AyatBySura.ToObservableCollection();

            foreach (var ayat in this.AyaBySuraList)
            {
                for (int i = 2; i <= ayat[0].NumberOfAyat; i++)
                {
                    SuraViewModel newAya = new SuraViewModel()
                    {
                        SuraNumber = ayat[0].SuraNumber,
                        SuraEnglishName = ayat[0].SuraEnglishName,
                        SuraArabicName = ayat[0].SuraArabicName,
                        NumberOfAyat = ayat[0].NumberOfAyat,
                        AyaNumber = i
                    };

                    ayat.Add(newAya);
                }
            }

            this.SuraListGroup.ItemsSource = this.AyaBySuraList;
        }

        private void SuraListGroup_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            LongListSelector s = (LongListSelector)sender;
            SuraViewModel v = (SuraViewModel)s.SelectedItem;

            if (v != null)
            {
                //MessageBox.Show(v.AyaNumber.ToString());
                BookmarkSuraKey = v.SuraNumber;
                BookmarkAyaNumber = v.AyaNumber;
            }
        }
    }

    public class GroupedOC<T> : ObservableCollection<T>
    {
        public GroupedOC(int name, IEnumerable<T> items)
        {
            this.Key = name;
            foreach (T item in items)
            {
                this.Add(item);
            }
        }

        public override bool Equals(object obj)
        {
            GroupedOC<T> that = obj as GroupedOC<T>;

            return (that != null) && (this.Key.Equals(that.Key));
        }

        public int Key
        {
            get;
            set;
        }

        public bool HasItems
        {
            get
            {
                return (Count != 0);
            }
        }
    }

    public static class EnumerableExtensions
    {
        // this is a convenience extension method that creates a new ObservableCollection<T>
        // given an IEnumerable<T>
        public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> collection)
        {
            var observableCollection = new ObservableCollection<T>();

            foreach (T item in collection)
            {
                observableCollection.Add(item);
            }

            return observableCollection;
        }
    }
}


